import VueRouter from 'vue-router'
import Store from './store/index'
import jwtToken from './helpers/jwt'

import {mapState,mapMutations,mapGetters,mapActions} from 'vuex';

let routes = [
  {
    path: "/",
    name: "home",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/index/index"], resolve) //首页
    // component:login00,
    // meta: {}
  },
  {
    path: "/userCore", //用户中心
    name: "userCore",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/userCore/userCore"], resolve), //用户中心
    meta: { requiresAuth: true }
  },
  {
    path: "/aboutJs", //关于我们
    name: "aboutJs",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/about/aboutTz"], resolve),
    meta: {}
  },
  {
    path: "/author", //作者主页
    name: "author",
    // component: require("./components/author/author")
    component: resolve => require(["./components/author/author"], resolve)
    // meta: { requiresAuth: true }
  },
  {
    path: "/modeInfo", //3D模型详情
    name: "modeInfo",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/modeInfo/modeInfo"], resolve)
  },
  {
    path: "/ModeInfoSu", //SU模型详情
    name: "ModeInfoSu",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/modeInfo/modeInfoSu"], resolve)
  },
  {
    path: "/modeInfoConcept", //概念方案详情
    name: "modeInfoConcept",
    // component: require("./components/pages/Home"),
    component: resolve =>
      require(["./components/modeInfo/modeInfoConcept"], resolve)
  },
  {
    path: "/modeInfoDefinition", //高清案例详情
    name: "modeInfoDefinition",
    // component: require("./components/pages/Home"),
    component: resolve =>
      require(["./components/modeInfo/modeInfoDefinition"], resolve)
  },
  {
    path: "/modeInfoConstr", //施工图详情
    name: "modeInfoConstr",
    // component: require("./components/pages/Home"),
    component: resolve =>
      require(["./components/modeInfo/modeInfoConstr"], resolve)
  },
  /* {
    path: "/modeInfoMater", //材质贴图详情
    name: "modeInfoMater",
    // component: require("./components/pages/Home"),
    component: resolve =>
      require(["./components/modeInfo/modeInfoMater"], resolve)
  },
  {
    path: "/modeInfoBudget/:modeId", //预算模板详情
    name: "modeInfoBudget",
    // component: require("./components/pages/Home"),
    component: resolve =>
      require(["./components/modeInfo/modeInfoBudget"], resolve)
  }, */
  {
    path: "/concept", //概念方案
    name: "concept",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/concept/concept"], resolve)
  },
  /* {
    path: "/budget", //预算方案
    name: "budget",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/budget/budget"], resolve)
  }, */
  {
    path: "/cad", //施工图
    name: "constr",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/constr/constr"], resolve)
  },
  {
    path: "/works", //高清案例
    name: "definition",
    // component: require("./components/pages/Home"),
    component: resolve =>
      require(["./components/definition/definition"], resolve)
  },
  {
    path: "/model", //3D模型
    name: "threeMode",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/threeMode/three3d"], resolve)
  },
  {
    path: "/material", //材质贴图
    name: "czMap",
    // component: require("./components/pages/Home"),
    component: resolve => require(["./components/czMap/czMap"], resolve)
  },
  /* {
    path: "/recharge", //old充值中心
    name: "Recharge",
    component: resolve => require(["./components/recharge/recharge"], resolve)
    // meta: { requiresAuth: true }
  }, */
  {
    path: "/recharge", //充值中心
    name: "Recharge",
    component: resolve =>
      require(["./components/rechargeNew/recharge"], resolve)

    // meta: { requiresAuth: true }
  },
  {
    path: "/sumodel", //Sk模型
    name: "skMode",
    component: resolve => require(["./components/SKmode/skMode"], resolve)
  },
  {
    path: "/uploadIndex", //上传模型
    name: "uploadIndex",
    component: resolve =>
      require(["./components/uploadMode/uploadIndex"], resolve),
    meta: { requiresAuth: true }
  },
  {
    path: "/downTools", //下载软件
    name: "downTools",
    component: resolve => require(["./components/downTools/downTools"], resolve)
  },
  {
    path: "/vipIndex", //下载软件
    name: "vipIndex",
    component: resolve => require(["./components/vipList/vip"], resolve)
  },
  {
    path: "/share", //分享
    name: "share",
    component: resolve => require(["./components/share/share"], resolve)
  },
  {
    path: "/shareLogin", //分享注册
    name: "shareLogin",
    component: resolve =>
      require(["./components/shareLogin/shareLogin"], resolve)
  },
  {
    path: "/mobile", //移动端分享
    name: "mobile",
    component: resolve => require(["./components/mobile/mobile"], resolve)
  },
  /* {
    path: "/about",
    component: require("./components/pages/About"),
    meta: {}
  },
  {
    path: "/posts/:id",
    name: "posts",
    component: require("./components/posts/Post"),
    meta: {}
  },
  {
    path: "/register",
    name: "register",
    component: require("./components/register/Register"),
    meta: { requiresGuest: true }
  },
  {
    path: "/login",
    name: "login",
    component: require("./components/login/Login"),
    meta: { requiresGuest: true }
  },
  {
    path: "/confirm",
    name: "confirm",
    component: require("./components/confirm/Email"),
    meta: {}
  },
  {
    path: "/profile",
    component: require("./components/user/ProfileWrapper"),
    children: [
      {
        path: "",
        name: "profile",
        component: require("./components/user/Profile"),
        meta: { requiresAuth: true }
      },
      {
        path: "/edit-profile",
        name: "profile.editProfile",
        component: require("./components/user/EditProfile"),
        meta: { requiresAuth: true }
      },
      {
        path: "/edit-password",
        name: "profile.editPassword",
        component: require("./components/password/EditPassword"),
        meta: { requiresAuth: true }
      }
    ],
    meta: { requiresAuth: true }
  }, */
  {
    path: "/rechargeSuccess",
    name: "rechargeSuccess",
    component: require("./components/rechargeSuccess")
  },
  {
    path: "/rechargeError",
    name: "rechargeError",
    component: require("./components/rechargeError")
  },
  {
    path: "/error",
    name: "error",
    component: require("./components/error")
  },
  {
    path: "*",
    component: require("./components/error")
  }
];

const router = new VueRouter({
    mode: 'history',
    routes
})

 router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth) {
        if (Store.state.AuthUser.authenticated || jwtToken.getToken()) {
            // Store.commit('loadIngShows');//loading显示
           return next({'name':to})
        } else {
            next({ name: "home" });
            Store.commit("loginShow",to);
            return next(false);
        }
         
    }
    if (to.meta.requiresGuest) {
        if (Store.state.AuthUser.authenticated || jwtToken.getToken()) {
            return next({'name': 'home'})
        } else {
            return next()
        }
    }
    Store.commit('loadIngShows');
    next();
}) 
router.afterEach((to, from) => {
  // Store.commit("loadIngShows");
  Store.commit('loadIngHides')//loading隐藏
   /* setTimeout(() => {
      var _hmt = _hmt || [];
      (function() {
        //每次执行前，先移除上次插入的代码
          document.getElementById('baidu_tj') && document.getElementById('baidu_tj').remove();
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?6d21103d299c5aa8b0952777b6f70bc8";
          hm.id = "baidu_tj"
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
      })();
    }, 0); */

    
    
});
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
    router.replace(targetPath);
  }
})
export default router
